<template>
<body>
  <div id="register">
    <div class="r-header">
      <router-link to="/">
        <img src="../../assets/logo-white.png" alt />
      </router-link>
    </div>
    <div class="r-nav">
      <div class="r-nav-form">
        <el-form
          :model="ruleForm"
          status-icon
          :rules="rules"
          ref="ruleForm"
          label-width="70px"
          class="demo-ruleForm"
        >
          <el-form-item label="昵称" prop="name">
            <el-input type="text" name="userName" v-model="ruleForm.name" autocomplete="off"></el-input>
          </el-form-item>
          <el-form-item label="密码" prop="pass">
            <el-input type="password" name="userPwd" v-model="ruleForm.pass" autocomplete="off"></el-input>
          </el-form-item>
          <el-form-item>
            <router-link to="/register" tag="p" class="r-login">无账号，去注册></router-link>
          </el-form-item>

          <el-form-item class="r-res">
            <el-button type="primary" @click.prevent="submitForm('ruleForm')">登录</el-button>
            <el-button @click="resetForm('ruleForm')">重置</el-button>
          </el-form-item>
        </el-form>
      </div>
    </div>
    <div class="r-footer">
      <a href>版权所有@食界 |技术支持@食界IT部</a>
    </div>
  </div>
</body>
</template>
<script>
export default {
  data() {
    var validateText = (rule, value, callback) => {
      if (value === "") {
        callback(new Error("请输入昵称"));
      } else {
        if (this.ruleForm.checkPass !== "") {
          this.$refs.ruleForm.validateField("checkPass");
        }
        callback();
      }
    };
    var validatePass = (rule, value, callback) => {
      if (value === "") {
        callback(new Error("请输入密码"));
      } else {
        if (this.ruleForm.checkPass !== "") {
          this.$refs.ruleForm.validateField("checkPass");
        }
        callback();
      }
    };

    return {
      ruleForm: {
        name: "",
        pass: ""
      },
      rules: {
        name: [{ validator: validateText, trigger: "blur" }],
        pass: [{ validator: validatePass, trigger: "blur" }]
      }
    };
  },
  created(){
    if (sessionStorage.getItem('newUser')) {
       this.ruleForm.name=(JSON.parse(sessionStorage.getItem('newUser')).userName)
        this.ruleForm.pass=(JSON.parse(sessionStorage.getItem('newUser')).userPwd)
    }
  },
  methods: {
    submitForm(formName) {
      // this.$refs[formName].validate((valid) => {
      //   if (valid) {
      //     alert("submit!");
      //   } else {
      //     console.log("error submit!!");
      //     return false;
      //   }
      // });
      this.$axios.post('/api/users/userLogin',{
      userName:this.ruleForm.name,userPwd:this.ruleForm.pass})
      .then((res) => {
          console.log("token:", res);
          //保存到本地
          localStorage.setItem("MyToken", res.data.data);
          if (res.data.data == 0) {
            alert("密码错误");
          } else {
            this.$router.push("/");
          }
          //跳转到首页
        })
        .catch(err => {
          console.log("err:", err);
        });
    },

    resetForm(formName) {
      this.$refs[formName].resetFields();
    }
  }
};
</script>
<style lang="css" scoped>
@import "../../css/menu/login.css";
</style>

<style lang="css"  scoped>
 body {
    width: 100%;
    height: 100%;
    background-image: url(../../assets/login_bg.jpg);
    background-repeat: no-repeat;
    background-size: cover;
    background-attachment: fixed;
  }
#register .el-form-item__label {
  color: rgb(207, 202, 202);
}
</style>

